package io.github.uicdb.et.compat;

import com.google.common.collect.Maps;
import io.github.uicdb.et.ElementalTheory;
import io.github.uicdb.et.ModBlocks;
import io.github.uicdb.et.ModElements;
import io.github.uicdb.et.element.BasicElement;
import io.github.uicdb.et.recipe.ExtractRecipe;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IRecipeSlotDrawable;
import mezz.jei.api.gui.widgets.IRecipeExtrasBuilder;
import mezz.jei.api.gui.widgets.IRecipeWidget;
import mezz.jei.api.gui.widgets.ISlottedWidgetFactory;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.navigation.ScreenPosition;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.AbstractCookingRecipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ExtractRecipeCategory implements IRecipeCategory<ExtractRecipe> {
    public static final RecipeType<ExtractRecipe> RECIPE_TYPE = RecipeType.create(ElementalTheory.MODID, "extract_recipe", ExtractRecipe.class);

    // png file texture
    public static final ResourceLocation TEXTURE = ResourceLocation.fromNamespaceAndPath(ElementalTheory.MODID,
            "textures/gui/extractor_background.png");

    // 合成分类的背景图片
    private final IDrawable background;
    // 合成分类的图标
    private final IDrawable icon;

    public ExtractRecipeCategory(IGuiHelper helper) {
        // 渲染背景图片。图片的开始位置和图片的结束的位置 u,v,width,height
        this.background = helper.createDrawable(TEXTURE, 0, 0, 220, 85);
        // 图标
        this.icon = helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, new ItemStack(ModBlocks.EXTRACT_MACHINE));
    }

    @Override
    public @NotNull RecipeType<ExtractRecipe> getRecipeType() {
        return RECIPE_TYPE;
    }

    @Override
    public Component getTitle() {
        return Component.translatable("gui.extract_recipe.title");
    }

    @Override
    public IDrawable getBackground() {
        return background;
    }

    @Override
    public @Nullable IDrawable getIcon() {
        return icon;
    }

    @Override
    public void setRecipe(IRecipeLayoutBuilder builder, ExtractRecipe recipe, IFocusGroup focuses) {
        builder.addSlot(RecipeIngredientRole.INPUT, 20, 20).addIngredients(recipe.getIngredient());
        builder.addSlotToWidget(RecipeIngredientRole.OUTPUT, (ISlottedWidgetFactory<ExtractRecipe>) (builder1, recipe1, slots) -> {
            Map<BasicElement, Integer> integerMap = recipe1.getResult().stream().map(
                    a ->
                            new AbstractMap.SimpleEntry<>(a.element().value(), a.amount())
            ).collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
            for (int i = 0; i < ModElements.values().length; i++) {
                ModElements elem = ModElements.values()[i];
                Component component = Component.literal(elem.getChinese() + ":" + integerMap.getOrDefault(elem.holder().value(), 0));
                builder1.addWidget(new ExtractRecipeRecipeWidget(component,new ScreenPosition(1+i*15,20)));
            }
        });
    }

    private static class ExtractRecipeRecipeWidget implements IRecipeWidget {
        private final ScreenPosition position;
        private final Component component;

        public ExtractRecipeRecipeWidget(Component component, ScreenPosition position) {
            this.component = component;
            this.position = position;
        }

        @Override
        public @NotNull ScreenPosition getPosition() {
            return position;
        }

        @Override
        public void draw(GuiGraphics guiGraphics, double mouseX, double mouseY) {
            guiGraphics.drawString(Minecraft.getInstance().font, component, position.x(), position.y(), ChatFormatting.RED.getColor());
        }
    }
}
